import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;


public class Veranstaltungswahl extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel();
	private JPanel jPanel1 = new JPanel(null, true);
    private JLabel jLabel2 = new JLabel();
    private JPanel jPanel2 = new JPanel(null, true);
    private JPanel jPanel4 = new JPanel(null, true);
    private JButton jStornieren = new JButton("Weiter zur Platzwahl");
    private JLabel jLabel1 = new JLabel();
    private JPanel jPanel3 = new JPanel(null, true);
    private JTable jTable1 = new JTable(100, 4);
    private DefaultTableModel jTable1Model = (DefaultTableModel) jTable1.getModel();
    private JScrollPane jTable1ScrollPane = new JScrollPane(jTable1);
    Database db;
    User user;

	/**
	 * Launch the application.
	 *

	/**
	 * Create the dialog.
	 */
	public Veranstaltungswahl(Database db, User userObj) throws SQLException {
setTitle("Veranstaltungswahl");
		setBounds(100, 100, 780, 444);
		setVisible(true);
		this.user=userObj;
	    this.db=db;
	    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	    int x = (d.width - getSize().width) / 2;
	    int y = (d.height - getSize().height) / 2;
	    setLocation(x, y);
	    setResizable(false);
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenuItem mntmKinoverwaltung = new JMenuItem("Kinoverwaltung");
		mntmKinoverwaltung.setFont(new Font("Verdana", Font.PLAIN, 14));
		mntmKinoverwaltung.addActionListener(this);
		menuBar.add(mntmKinoverwaltung);

		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		panel.setForeground(Color.WHITE);
		panel.setBackground(new Color(51, 153, 204));
		
		JLabel lblTakeaseatVeranstaltungswahl = new JLabel("Take-A-Seat Veranstaltungswahl");
		lblTakeaseatVeranstaltungswahl.setForeground(Color.WHITE);
		lblTakeaseatVeranstaltungswahl.setFont(new Font("Arial Black", Font.PLAIN, 22));
		lblTakeaseatVeranstaltungswahl.setBackground(SystemColor.inactiveCaption);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGap(0, 564, Short.MAX_VALUE)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblTakeaseatVeranstaltungswahl, GroupLayout.PREFERRED_SIZE, 426, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(132, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGap(0, 54, Short.MAX_VALUE)
				.addComponent(lblTakeaseatVeranstaltungswahl, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
		);
		panel.setLayout(gl_panel);
		
		jTable1.setFont(new Font("Verdana", Font.PLAIN, 12));
		jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jTable1.getColumnModel().getColumn(0).setHeaderValue("Nr.");
	    jTable1.getColumnModel().getColumn(1).setHeaderValue("Film");
	    jTable1.getColumnModel().getColumn(2).setHeaderValue("Datum");
	    jTable1.getColumnModel().getColumn(3).setHeaderValue("Zeit");
	    
ResultSet rs = db.getData("SELECT VeranstaltungsID,FilmID,Datum,Zeit FROM veranstaltungen");
	    
	    int row=0;
	    	    
	    while(rs.next()){
	    	    jTable1.setValueAt(rs.getString(1),row,0);
	    	    ResultSet rsFilm = db.getData("SELECT Filmtitel FROM filme WHERE Filmnummer = " + Integer.parseInt(rs.getString(2)));
	    	    rsFilm.next();
	    	    jTable1.setValueAt(rsFilm.getString(1),row,1);
	    	    jTable1.setValueAt(rs.getString(3),row,2);
	    	    jTable1.setValueAt(rs.getString(4),row,3);
	    	    //System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
	    	    row++;
	  }
	    
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, 764, Short.MAX_VALUE)
				.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
					.addGap(43)
					.addComponent(jTable1ScrollPane, GroupLayout.PREFERRED_SIZE, 675, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(46, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(jTable1ScrollPane, GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
					.addContainerGap())
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(new Color(51, 153, 204));
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Auswahl best\u00E4tigen");
				okButton.setFont(new Font("Verdana", Font.PLAIN, 14));
				okButton.setActionCommand("Ok");
				buttonPane.add(okButton);
				okButton.addActionListener(this);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Abbrechen");
				cancelButton.setFont(new Font("Verdana", Font.PLAIN, 14));
				cancelButton.setActionCommand("Abbrechen");
				cancelButton.addActionListener(this);

				buttonPane.add(cancelButton);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		if (evt.getActionCommand()=="Ok"){
			try {
				jStornieren_ActionPerformed(evt);
			} catch (NumberFormatException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(evt.getActionCommand()=="Kinoverwaltung"){
			dispose();
			try {
				new Kinoverwaltung(db, user);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(evt.getActionCommand()=="Abbrechen"){
			try {
				dispose();
				Kinoverwaltung Kv=new Kinoverwaltung(db, user);
				Kv.setVisible(true);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
		// TODO Auto-generated method stub
		
public void jStornieren_ActionPerformed(ActionEvent evt) throws NumberFormatException, SQLException {
	  int temp = Integer.parseInt((String) jTable1.getValueAt(jTable1.getSelectedRow(),0));
	  System.out.println("ID der Veranstaltung ist: " + temp);
	  Veranstaltung vfr = new ConvertTo().Veranstaltung(db, temp);
	  dispose();
    Sitzplatzverwaltung tas= new Sitzplatzverwaltung(db,vfr,user);
	tas.setExtendedState(JFrame.MAXIMIZED_BOTH);
	tas.setMaximizedBounds(getBounds());
} 
}
